var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var app = getApp();
var page = 10; 
var key = "";    
 
var loadMore = function (that) {    
       
      key = that.data.key ? that.data.key:wx.getStorageSync('key');
      if (that.data.order_group_list) {
        var listBefore = that.data.order_group_list;
      } else {
        var listBefore = new Array();
      }        
      that.setData({
        showLoading: false
      });
     
      wx.request({
        url: ApiUrl + "/index.php?act=member_order&op=order_list&page=10&curpage="+that.data.curpage,
        data: {
          'key': key,
          'state_type': '',
          'order_key': ''
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
 
          var ordergroup = res.data.datas.order_group_list;
          for (var i = 0; i < ordergroup.length; i++) {
              var list = ordergroup[i]['order_list'];
              for (var o = 0; o < list['length']; o++) {
                  if (list[o]['order_state'] == 20 || list[o]['order_state'] == 30 || list[o]['order_state'] == 40) {
                    ordergroup[i]['order_list'][o]['stateClass'] = "ot-finish";
                  } else if (list[o]['order_state'] == 0) {
                    ordergroup[i]['order_list'][o]['stateClass'] = "ot-cancel";
                  } else {
                    ordergroup[i]['order_list'][o]['stateClass'] = "ot-nofinish";
                  }
                  var goods_num = 0;
                  for (var j = 0; j < list[o].extend_order_goods.length; j++) {               
                    goods_num = parseInt(goods_num) + parseInt(list[o].extend_order_goods[j]['goods_num']);                
                  }               
                  ordergroup[i]['order_list'][o]['goods_num'] = goods_num;
              }
          }
       
          var curpage = that.data.curpage+1;
//console.log(res.data.datas);
          that.setData({
            order_group_list: listBefore.concat(ordergroup),
            showLoading: true,
            hasmore: res.data.hasmore,
            curpage: curpage,
            key:key
          });
         
        },
      });
}

Page({
  /**
   * 页面的初始数据
   */
  data: {        
    showLoading: true,
    scrollHeight: 0,
    scrollTop: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      key = wx.getStorageSync('key');
      
      
      var that = this;
      var curpage = 1;    
      wx.getSystemInfo({
        success: function (result) {
          that.setData({
            scrollHeight: result.windowHeight,         
            curpage: curpage,
            key:key
          });  
          
          if (key) {  
            loadMore(that); 
          }else{                 
            wx.reLaunch({
              url: '/pages/index/index',             
            })             
          }
      },
  });
  },
  topay: function (option) {
    key = wx.getStorageSync('key');
    var pay_sn = option.currentTarget.dataset.paysn;
 
    var openid = wx.getStorageSync('openid');
    wx.request({
      url: ApiUrl + '/index.php?act=member_payment&op=pay_weixin',//改成你自己的链接
      data: { 'openid': openid, 'pay_sn': pay_sn, 'key': key },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
//console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp.toString(),
          'nonceStr': res.data.nonceStr,
          'package': "prepay_id=" + res.data.package2,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 3000
            });
          },
          'fail': function (res) {
            wx.showToast({
              title: '支付失败',
              icon: 'success',
              duration: 3000
            });
          },
          'complete': function (res) {
            wx.reLaunch({
              url: '/pages/order/order',
            })
           
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '网络错误 请稍后再试~',
          icon: 'success',
          duration: 3000
        });
      }
    });
  },
  //页面滑动到底部
  bindDownLoad: function () {
    var that = this;
    if (that.data.hasmore) {
      loadMore(that);
    } else {
      return false;
    }
  },
  deleteOrder:function(param){
    var that = this;     
    var order_id = param.currentTarget.dataset.order_id;
    key = that.data.key;
    wx.showModal({
      title: '提示',
      content: '是否移除订单？电脑端订单回收站可找回订单！',
      success: function (res) {
        if (res.confirm) {
            wx.request({
              url: ApiUrl + "/index.php?act=member_order&op=order_delete",
              data: { 'order_id': order_id, 'key': key },
              header: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              method: 'post',
              success: function (res) {  
              
                 if(res.data.code=="200"){
                   wx.reLaunch({
                     url: '/pages/order/order',
                   })
                   
                 }else{
                   wx.showToast({
                     title: res.data.datas.error,
                     icon: 'success',
                     duration: 4000
                   })
                 }
              }
            })
        } 
      }
    })
   
  },
  //确认收货
  sure_order: function (result) {
    var that = this;
    key = that.data.key;
    var order_id = result.currentTarget.dataset.order_id;

    wx.showModal({
      title: '',
      content: '确定收到货物?',
      success: function (res) {
        if (res.confirm) {
            wx.request({
              url: ApiUrl + "/index.php?act=member_order&op=order_receive",
              data: { 'key': key, 'order_id': order_id },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: 'post',
              success: function (res) {
                if (res.data.datas == 1) {
                  wx.showToast({
                    title: 'ok',
                    icon: 'success',
                    duration: 2000
                  })
                  wx.reLaunch({
                    url: '/pages/order/order',
                  })
                  
                }
              }
            })
        }
      }
    })
  },
  //取消订单
  cancel_order: function (result) {
    var that = this;
    key = that.data.key;
    
    var order_id = result.currentTarget.dataset.order_id; 
    wx.showModal({
      title: '',
      content: '确定要取消订单',
      success: function (res) {
        if (res.confirm) {

          wx.request({
            url: ApiUrl + "/index.php?act=member_order&op=order_cancel",
            data: { 'key': key, 'order_id': order_id,"type":'xiaochengxu' },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'post',
            success: function (res) {
              
              if(res.data.code==400){
                var error = '';
                if (res.data.datas.error == "该订单曾尝试使用第三方支付平台支付，须在24小时以后才可取消") {
                  error = '24小时以后重试';
                }else{
                  error = res.data.datas.error;
                }
                  wx.showToast({
                    title: error,
                    icon: 'success',
                    duration: 4000
                  }) 
              }else{
                wx.reLaunch({
                  url: '/pages/order/order',
                })
                  
              }               
             }
          })
        }
      }
    })
  },
});